/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.AppEntity;
import io.renren.modules.sys.entity.Menu;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.service.SysMenuService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 系统菜单
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月27日 下午9:58:15
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 导航菜单
	 */
	@RequestMapping("/nav")
	public R nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		return R.ok().put("menuList", menuList);
	}
	
	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public List<SysMenuEntity> list(){
		List<SysMenuEntity> menuList = sysMenuService.selectList(null);
		for(SysMenuEntity sysMenuEntity : menuList){
			SysMenuEntity parentMenuEntity = sysMenuService.selectById(sysMenuEntity.getParentId());
			if(parentMenuEntity != null){
				sysMenuEntity.setParentName(parentMenuEntity.getName());
			}
		}

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public R select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return R.ok().put("menuList", menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public R info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.selectById(menuId);
		return R.ok().put("menu", menu);
	}

	/**
	 * 获取一级菜单  如果是超管 所有菜单都显示  如果是普通用户 显示已有权限的菜单
	 * @return
	 */
	@RequestMapping("/findDesk")
	public R findDesk(){
		try{

			List<SysMenuEntity> docks = new ArrayList<>();
			List<SysMenuEntity> desks = new ArrayList<>();

			//系统管理员，拥有最高权限
			Long userId = getUserId();
			if(userId == Constant.SUPER_ADMIN){
				docks = sysMenuService.selectList(new EntityWrapper<SysMenuEntity>()
						.eq("type", "0")//目录
						.eq("is_desk", "0")//一级
				);
				desks = sysMenuService.selectList(new EntityWrapper<SysMenuEntity>()
						.eq("type", "0")//目录
						.eq("is_desk", "1")//一级
				);
			}else{
				HashMap<String, Object> map = new HashMap<>();
				map.put("type",0);
				map.put("isDesk",0);
				map.put("userId",userId);
				docks = sysMenuService.getUserMenuListByMenuId(map);
				map.put("isDesk",1);
				desks = sysMenuService.getUserMenuListByMenuId(map);
			}

			return R.ok().put("desks",desks).put("docks",docks);
		}catch (Exception e){

		}


		return R.ok();
	}

	/**
	 * 获取子菜单  如果是超管 所有菜单都显示  如果是普通用户 显示已有权限的菜单
	 * @param menuId
	 * @return
	 */
	@RequestMapping("/findMenuByDesk")
	public ArrayList<Menu> findMenuByDesk(Long menuId){

		ArrayList<Menu> menus1 = new ArrayList<>();
		try{

			SysMenuEntity sysMenuEntity = sysMenuService.selectById(menuId);

			List<SysMenuEntity> menus = new ArrayList<>();

			Long userId = ShiroUtils.getUserId();
			//系统管理员，拥有最高权限
			if(userId == Constant.SUPER_ADMIN){
				menus = sysMenuService.selectList(new EntityWrapper<SysMenuEntity>()
						.eq("parent_id", menuId)//目录
						.eq("type", "1")); //一级
			}else{
				HashMap<String, Object> map = new HashMap<>();
				map.put("userId",userId);
				map.put("parentId",menuId);
				menus = sysMenuService.getUserMenuListByMenuId(map);
			}


			List<Menu.ChildrenBean> childrens = new ArrayList<>();

			for(SysMenuEntity sysMenuEntity1 :menus){
				Menu.ChildrenBean childrenBean = new Menu.ChildrenBean();
				childrenBean.setId(sysMenuEntity1.getMenuId());
				childrenBean.setText("<a onclick=\"NewTab('" + sysMenuEntity1.getName() + "','../../" + sysMenuEntity1.getUrl() + "','" + sysMenuEntity1.getMenuId() + "')\">" + sysMenuEntity1.getName() + "</a>");
				childrens.add(childrenBean);
			}

			Menu menu = new Menu();
			menu.setId(menuId);
			menu.setState("open");
			menu.setText(sysMenuEntity.getName());
			menu.setChildren(childrens);
			menus1.add(menu);
			return menus1;
		}catch (Exception e){

		}
		return menus1;
	}


	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@RequestMapping("/save")
	@RequiresPermissions("sys:menu:save")
	public R save(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		
		sysMenuService.insert(menu);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@RequestMapping("/update")
	@RequiresPermissions("sys:menu:update")
	public R update(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
				
		sysMenuService.updateById(menu);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:menu:delete")
	public R delete(long menuId){
		if(menuId <= 31){
			return R.error("系统菜单，不能删除");
		}

		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return R.error("请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return R.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RRException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new RRException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RRException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.selectById(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new RRException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
